/*
 * @Description: 用户权限的提取
 * @Author: Liu Kunpeng
 * @Date: 2021-08-01 10:24:03
 */

import store from "@/store"
import { asyncRoutes } from "@/router"
import { title } from "@/settings";
/**
 * @description: 
 * @param {*}
 * @return {*}
 */

export async function getMenu(){
    var permission = new Array()
    permission = store.getters.treePermission  //获取用户权限
    // console.log(permission);
    if(permission.length === 0){
        console.log('获取');
        await store.dispatch('user/getTreePermission')
    }
    permission = store.getters.treePermission
    // console.log(permission);
    const parentMenu = permission[0]
    var menuFolder = new Array()
    var menuItem = new Array()
    var menuFunction = new Array()
    if(parentMenu.description === "全部数据"){
        const childrenMenu = parentMenu.children
        childrenMenu.forEach(element => {//对主菜单栏进行提取
            menuFolder.push(element.description)
            element.children.forEach(e=>{ //对副菜单栏进行提取
                menuItem.push(e.description)
                e.children.forEach(i=>{//对可用的api功能进行提取
                    menuFunction.push(i.description)
                })
            })
        });
    }else{
        permission.forEach(element=>{
            menuFolder.push(element.description)    //菜单文件夹
            element.children.forEach(e=>{
                menuItem.push(e.description)
                e.children.forEach(i=>{//对可用的api功能进行提取
                    menuFunction.push(i.description)
                })
            })
        })
    }
    
    var menu = new Object()
    menu.menuFolder = menuFolder
    menu.menuItem = menuItem
    menu.menuFunction = menuFunction
    // console.log(menu);////获取菜单栏上的主菜单栏和副菜单栏文字对象
    return menuItem  //获取菜单栏上的副菜单栏文字
}

function hasPermisssion(menuItem,route){ //副菜单栏与路由的子标题是否相等
    var flag = false
    route.children.forEach(element => {
        const title = element.meta.title
        if(menuItem.indexOf(element.meta.title) !== -1){//title在element.meta.title里就将flag设为true
            flag = true
        }else{
            // console.log(-1);
        }

    });
    return flag
}
export function filterAsyncRouter(menuItem){
    const accessedRouters = asyncRoutes.filter(route=>{
        if(hasPermisssion(menuItem,route)){
            return true //全端的动态路由中的route若在menuitem里，就返回true
        }else{
            return false
        }
    })
    return accessedRouters
}
